//
//  ContentEditerViewController.m
//  ZYHome
//
//  Created by Teplot_03 on 16/7/6.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ContentEditorViewController.h"
#import "PublicNewsViewController.h"
//#import "PublicBizController.h"

@interface ContentEditorViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *limitCharL;

@end

@implementation ContentEditorViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    [_textView becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑内容";
    
    [self initUI];
}

- (void)initUI {
    self.view.backgroundColor = LighterGrayColor;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    
    //添加文本
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight*2/5)];
    _textView.font = [UIFont systemFontOfSize:17];
    _textView.text = self.text;
    [self.view addSubview:_textView];
    
    _limitCharL = [[UILabel alloc] init];
    _limitCharL.text = @"您还可以输入2000个字";
    _limitCharL.font = [UIFont systemFontOfSize:13];
    _limitCharL.frame = CGRectMake(10, CGRectGetMaxY(_textView.frame), KScreenWidth, 20);
    _textView.delegate = self;
    [self.view addSubview:_limitCharL];
}

#pragma mark - 导航栏两边按钮点击
- (void)leftItemClick {
//    self.pubBizVC.backType = BackTypeCancle;
    self.pubNewsVC.backType = BackTypeCancle;
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)rightItemClick {
    
    if (self.textView.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入有效内容"];
        return;
    }
    
//    self.pubBizVC.backType = BackTypeEnsure;
    self.pubNewsVC.backType = BackTypeEnsure;
    
//    self.pubBizVC.changeString = self.textView.text;
    self.pubNewsVC.changeString = self.textView.text;
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - textView delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (![text  isEqual: @""]) {
        NSInteger numOfChar = textView.text.length;
        if (2000-numOfChar<=0) {
            return NO;
        }
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    NSInteger numOfChar = textView.text.length;
    _limitCharL.text = [NSString stringWithFormat:@"您还可以输入%ld个字",2000-numOfChar];
}

#pragma mark - others
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end
